// index.js
// 获取应用实例
const app = getApp()
const dateWather= require('../../config/api');
//引入api
const API = require('../../config/api')
const utils = require('../../util/util')


let chart = null; //创建空表
 function initChart(canvas, width, height, F2) { // 使用 F2 绘制图表
  //创建图表对象
  chart = new F2.Chart({
    el: canvas,
    width,
    height
  });
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    opts: {
      onInit: initChart,
     
    },
    ChickenSoup: [], //毒鸡汤
    sevenDays: [], //未来7天
    dateWatherH:[],//24小时
    comfort:[] , //舒适度
    prompt:[],
    location: '108.312624,22.833925' //经纬度
  },

  onReady() {},
  /**
   * 生命周期函数--监听页面加载
   */


  onLoad: function(options) {
   
    //毒鸡汤
    wx.request({
      url: API.chickenSoup,
      method: 'POST',
      data: {
        chiose: 'ji'
      },
      success: (res) => {
        // console.log('文',res);
        this.setData({
          ChickenSoup: res.data
        })
      }
    })
  
      //解决异步同步问题
      var timer =setInterval(() => {
        if (chart) {
         
       
         clearInterval(timer)
    
        }

      }, 20)






  },
  //获取2h接口数据
  getWeightInfo() {

    // wx.request({
    //   url: API.dateWather, //24小时
    //   method: 'GET',
    //   data: {
    //     location: this.data.location
    //   },
    //   success: (res) => {
    //     //  console.log('index24h',res);
    //     this.setData({
    //       hourly: res.data
    //     })
    //     this.dateWatherChart(res.data.hourly) //调用函数
      
    //   }
    // });
  },

  //   创建图表 
  dateWatherChart(hourly) {
    var data = [] //定义数组
    var maxTemp = parseInt(hourly[0].temp); //最大值
    var minTemp = parseInt(hourly[0].temp); //最小值
    hourly.forEach(element => {
      maxTemp = parseInt(element.temp) > maxTemp ? parseInt(element.temp) : maxTemp;
      minTemp = parseInt(element.temp) > minTemp ? parseInt(element.temp) : minTemp;
      data.push({
        fxTime: utils.formatFxTime(element.fxTime),
        temp: parseInt(element.temp),
        text: element.text
      })

    });
    ////载入数据源
    //清楚
   chart.clear()
    chart.source(data, {
      fxTime: {
        tickCount: 5 //设置刻度点的数
      },
      temp: {
        formatter: function formatter(ivalue) {
          return ivalue + '℃'
        },
        ticks: [minTemp - 4, (minTemp + maxTemp) / 4, maxTemp + 2] //定坐标轴上刻度点的文本信息
      }
    });
    //鼠标悬停显示的信息
    chart.tooltip({
      showCrosshairs: true,
      showItemMarker: false,
      background: {
        radius: 2,
        fill: "gray",
        padding: [6, 10]
      },
      //文本显示
      onShow(ev) {
        const {
          items
        } = ev; //取中ev的item
        items[0].name = null;
        items[0].value = items[0].origin.fxTime + ':' + items[0].origin.temp + '°' + items[0].origin.text;
      }
    });
    //
   
    chart.axis('fxTime', {
      label: {
        fill: '#75ADC0'
      }
    });
    chart.axis('temp', {
      label: {
        fill: '#75ADC0'
      }
    });
    // 设置图表样式
    chart.line().position('fxTime*temp').color('orange');
    chart.point().position('fxTime*temp').style({
      lineWidth: 1,
      stroke: '#fff'
    });
    //渲染图表
    chart.render();
  
  },
  //点击跳转 
  leftIndex(){
    wx.navigateTo({
      url: '../../pages/lifeIndex/lifeIndex?day='+JSON.stringify(this.data.comfort),
      success: res => {
        console.log("跳转成功");
      },
      fail: () => {
        console.log("跳转失败");
      },

    })
  },

 //接收24小时
 cahnge:function(e){
  const {hourly} = e.detail
  this.setData({
    dateWatherH:e.detail

  })
  this.dateWatherChart(e.detail.hourly) //图标调用函数
},
  //接收未来7天
  sevenDays(e){
    this.setData({
      sevenDays:e.detail.daily
    })

  },
  //接收舒适度
  indexLife(e){
  this.setData({
    comfort:e.detail.daily
  })
    //  //提示随机数组
    var curprompt=[]
    for (let i = 0; i < this.data.comfort.length; i++) {
      var  num=Math.floor(Math.random()*this.data.comfort.length)
      var prompt1= this.data.comfort[num];
      var prompt2= this.data.comfort[num];
  
    }
  
     curprompt.push({
      prompt1:prompt1,
      prompt2:prompt2
     })
     this.setData({
      prompt:curprompt[0]
     })
  }
})